
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/*
 * frmMain.java
 *
 * Created on 13 April 2010, 02:40
 */



/**
 *
 * @author  Administrator
 */
public class frmAddQ extends javax.swing.JFrame {
    private String tempStr="";
    int magin=6;
    int distanceH=25,distanceW=31;
    boolean repaint=false;
    int _width;
    int _height;
    Timer t=new Timer(20, new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            if(pnlM.getWidth()==_width*distanceW+10 && pnlM.getHeight()==_height*distanceH+10)
                t.stop();
            else
            {
                changeDimention();
            }
        }
    });
    /** Creates new form frmMain */
    public frmAddQ() {
        initComponents();
        LoadAll();
    }
    void LoadAll()
    {
        pnlM.removeAll();
        pnlIdH.removeAll();
        pnlIdV.removeAll();
        pnlAH.removeAll();
        pnlAV.removeAll();
        pnlGYH.removeAll();
        pnlGYV.removeAll();
        pnlRowH.removeAll();
        pnlRowV.removeAll();
        //pnlM.setLayout(new GridLayout());
        _width=Integer.parseInt(txtH.getText());
        _height=Integer.parseInt(txtV.getText());
        JTextField temp=null;
        for(int i=0;i<_width;i++)
        {
            for(int j=0;j<_height;j++)
            {
                temp=CreateTextField();
                temp.setName("txt"+(i*_height+j));
                temp.setLocation(j*distanceW+magin, i*distanceH+magin);
                temp.setVisible(true);
                pnlM.add(temp);
            }
        }
        pnlM.setSize(new Dimension(_height*distanceW+15, _width*distanceH+15));
        pnlM.setPreferredSize(new Dimension(_height*distanceW+15, _width*distanceH+15));
        JLabel lblAnswerV=new JLabel();
        int n=(_width<_height)?_height:_width;
        String str="";
        for(int i=0;i<n;i++)
            str+="*";
        for(int i=0;i<_height;i++)
        {
            lblAnswerV=new JLabel();
            lblAnswerV.setText(standardText(str.substring(0,_width)));
            lblAnswerV.setSize(new Dimension(160, 19));
            lblAnswerV.setLocation(0, i*distanceH+magin);
            pnlAV.add(lblAnswerV);
            lblAnswerV=new JLabel();
            lblAnswerV.setText("Hàng dọc "+(i+1));
            lblAnswerV.setSize(new Dimension(85, 19));
            lblAnswerV.setLocation(0, i*distanceH+magin);
            pnlRowV.add(lblAnswerV);
            temp=new JTextField();
            temp.setSize(251, 19);
            temp.setLocation(0, i*distanceH+magin);
            pnlGYV.add(temp);
            temp =CreateTF(String.valueOf(i+1));
            temp.setLocation(magin+i*(magin+distanceH),0);
            pnlIdV.add(temp);
        }
        for(int i=0;i<_width;i++)//số cột.
        {       
            Character c=new Character((char)(65+i%26));
            if(Math.floor(i/26)>0)
                tempStr=c.toString()+Math.round(i/26);
            else
                tempStr=c.toString();
            lblAnswerV=new JLabel();
            lblAnswerV.setText(standardText(str.substring(0,_height)));
            lblAnswerV.setSize(new Dimension(160, 19));
            lblAnswerV.setLocation(0, i*distanceH+magin);
            pnlAH.add(lblAnswerV);
            lblAnswerV=new JLabel();
            lblAnswerV.setText(tempStr);
            lblAnswerV.setSize(new Dimension(85, 19));
            lblAnswerV.setLocation(0, i*distanceH+magin);
            pnlRowH.add(lblAnswerV);
            temp=new JTextField();
            temp.setSize(251, 19);
            temp.setLocation(0, i*distanceH+magin);
            pnlGYH.add(temp);
            temp =CreateTF(tempStr);
            temp.setLocation(0, i*distanceH+magin);          
            pnlIdH.add(temp);
        }
        changeDimention();
    }
    void changeDimention()
    {
        int h=distanceW * _height +2*magin;
        int v=distanceW * _width+2*magin;
        //pnlMGY.setSize(new Dimension(pnlMGY.getWidth(),(_width+_height)*distanceH+600));
        //thiết lập cho hàng dọc -chữ H
        pnlHN.setPreferredSize(new Dimension(pnlHN.getWidth(),_width   *distanceH+7*magin));
        pnlAH.setSize(new Dimension(pnlAH.getWidth(), h));
        pnlRowH.setSize(new Dimension(pnlRowH.getWidth(), h));
        pnlGYH.setSize(new Dimension(pnlGYH.getWidth(), h));
        
        pnlHD.setPreferredSize( new Dimension( pnlHD.getWidth(),h+20));
        
        pnlAV.setSize(new Dimension(pnlAV.getWidth(), v));
        pnlRowV.setSize(new Dimension(pnlRowV.getWidth(), v));
        pnlGYV.setSize(new Dimension(pnlGYV.getWidth(), v));
        pnlMGY.setSize(new Dimension(pnlHD.getWidth()+50,pnlHD.getHeight()+pnlHN.getHeight()));
        //pnlHN.setSize( new Dimension( pnlHN.getWidth(),222));
       // JOptionPane.showMessageDialog(this, pnlHD.getBounds());
        pnlIdH.setSize(pnlIdH.getWidth(),pnlM.getHeight());
        pnlIdV.setSize(pnlM.getWidth(),pnlIdV.getHeight());
        
//        pnl6.setSize(new Dimension(pnlM.getWidth()+pnlIdH.getWidth()+50, pnlM.getHeight()+pnlIdV.getHeight()+50));
//        pnlMGY.validate();
//        pnlHN.validate();
//        pnlM.validate();
//        pnlGYH.validate();
//        pnlAH.validate();
//        pnlAV.validate();
//        pnlGYV.validate();
//        pnlIdH.validate();
//        pnlIdV.validate();
    }
    JTextField CreateTF(String str)
    {
        JTextField temp=new JTextField();
        temp.setText(str);
        temp.setColumns(2);
        temp.setSize(new Dimension(27, 19));
        temp.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        temp.setEditable(false);
        //temp.setBorder(null);
        return temp;
    }
    JTextField CreateTextField()
    {
        JTextField temp=new JTextField();
        temp.setColumns(2);
        temp.setText("*");
        temp.setSize(27, 19);
        temp.setPreferredSize(new java.awt.Dimension(27, 19));
        temp.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        temp.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtFocusGained(evt);
            }
        });
        temp.addKeyListener(new java.awt.event.KeyAdapter() {
            @Override
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtKeyReleased(evt);
            }
        });
        temp.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtMouseClicked(evt);
            }
        });
        return temp;
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
        private void txtMouseClicked(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:
        JTextField temp=(JTextField)   evt.getComponent();
        temp.setSelectionStart(0);
        temp.setSelectionEnd(1);
    }
    String standardText(String str)
    {
        String s="Đáp án:  ",temp="";
        for(int i=0;i<str.length();i++)
            if(str.charAt(i)!='*')
                s+=str.charAt(i); 
            else
                temp+="  ";
        s+=temp+  "  Hay:  "+str;
        return s;
    }
    private void txtKeyReleased(java.awt.event.KeyEvent evt) {
        // TODO add your handling code here:
        JLabel lbl=null;
        JTextField temp=(JTextField)evt.getComponent();
        if(temp.getText().length()>1) {
            JOptionPane.showMessageDialog(this, "Các hạ chỉ được oánh 1 chữ thôi."+evt.getComponent().getName());
            temp.setText(tempStr);
        } 
        else {
            String str="";
            int point=Integer.parseInt(temp.getName().substring(3));
            for(int i=0;i<_width;i++)//lấy cho cột
            {
                temp=(JTextField)pnlM.getComponent(i*_height+point%_height);
                str+=temp.getText();
            }
            lbl=(JLabel)pnlAV.getComponent(point%_height);
            lbl.setText(standardText(str));
            temp=(JTextField)pnlGYV.getComponent(point%_height);
            temp.setText(str);
            str="";
            int tp=(int)Math.floor(point/_height)*_height;
            for(int i=tp;i<_height+tp;i++)//lấy cho hàng
            {
                temp=(JTextField)pnlM.getComponent(i);
                str+=temp.getText();
            }
            lbl=(JLabel)pnlAH.getComponent(point/_height);
            lbl.setText(standardText(str));
            temp=(JTextField)pnlGYH.getComponent(point/_height);
            temp.setText(str);
        }
    }

    private void txtKeyPressed(java.awt.event.KeyEvent evt) {
        // TODO add your handling code here:
        JTextField temp=(JTextField)   evt.getComponent();
        tempStr=temp.getText();
    }

    private void txtFocusGained(java.awt.event.FocusEvent evt) {
        // TODO add your handling code here:
        JTextField temp=(JTextField)   evt.getComponent();
        temp.setSelectionStart(0);
        temp.setSelectionEnd(1);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        txtTitle = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        btnAdd = new javax.swing.JButton();
        btnReset = new javax.swing.JButton();
        txtH = new javax.swing.JTextField();
        txtV = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        pnl6 = new javax.swing.JPanel();
        pnlIdH = new javax.swing.JPanel();
        pnlM = new javax.swing.JPanel();
        pnlIdV = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        pnlMGY = new javax.swing.JPanel();
        pnlHN = new javax.swing.JPanel();
        pnlRowH = new javax.swing.JPanel();
        pnlGYH = new javax.swing.JPanel();
        pnlAH = new javax.swing.JPanel();
        pnlHD = new javax.swing.JPanel();
        pnlGYV = new javax.swing.JPanel();
        pnlRowV = new javax.swing.JPanel();
        pnlAV = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        txtTitle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtTitleActionPerformed(evt);
            }
        });

        jLabel1.setText("Tiêu đề:");

        btnAdd.setText("Thêm");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnReset.setText("Xóa trường");
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });

        txtH.setText("3");
        txtH.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtHKeyReleased(evt);
            }
        });

        txtV.setText("3");
        txtV.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtVKeyReleased(evt);
            }
        });

        jLabel12.setText("Hàng ngang:");

        jLabel13.setText("Hàng dọc:");

        jButton1.setText("Đóng");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(91, 91, 91)
                        .add(btnAdd)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btnReset)
                        .add(36, 36, 36)
                        .add(jButton1))
                    .add(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel1)
                            .add(jLabel12)
                            .add(jLabel13))
                        .add(69, 69, 69)
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(txtV, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 61, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(txtH, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 61, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(txtTitle, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 275, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(txtTitle, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel12)
                            .add(txtH, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(txtV, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(25, 25, 25)
                        .add(jLabel13)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 54, Short.MAX_VALUE)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(btnAdd)
                    .add(btnReset)
                    .add(jButton1))
                .add(24, 24, 24))
        );

        jScrollPane1.setAutoscrolls(true);

        pnl6.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        pnl6.setMaximumSize(new java.awt.Dimension(350, 300));
        pnl6.setPreferredSize(new java.awt.Dimension(500, 400));
        pnl6.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        pnlIdH.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.UIManager.getDefaults().getColor("Panel.background"), javax.swing.UIManager.getDefaults().getColor("Panel.background")));

        org.jdesktop.layout.GroupLayout pnlIdHLayout = new org.jdesktop.layout.GroupLayout(pnlIdH);
        pnlIdH.setLayout(pnlIdHLayout);
        pnlIdHLayout.setHorizontalGroup(
            pnlIdHLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 26, Short.MAX_VALUE)
        );
        pnlIdHLayout.setVerticalGroup(
            pnlIdHLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 156, Short.MAX_VALUE)
        );

        pnl6.add(pnlIdH, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 40, 30, 160));

        pnlM.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.UIManager.getDefaults().getColor("Panel.background"), javax.swing.UIManager.getDefaults().getColor("Panel.background")));
        pnlM.setAutoscrolls(true);

        org.jdesktop.layout.GroupLayout pnlMLayout = new org.jdesktop.layout.GroupLayout(pnlM);
        pnlM.setLayout(pnlMLayout);
        pnlMLayout.setHorizontalGroup(
            pnlMLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 196, Short.MAX_VALUE)
        );
        pnlMLayout.setVerticalGroup(
            pnlMLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 156, Short.MAX_VALUE)
        );

        pnl6.add(pnlM, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 40, 200, 160));

        pnlIdV.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.UIManager.getDefaults().getColor("Panel.background"), javax.swing.UIManager.getDefaults().getColor("Panel.background")));
        pnlIdV.setAutoscrolls(true);

        org.jdesktop.layout.GroupLayout pnlIdVLayout = new org.jdesktop.layout.GroupLayout(pnlIdV);
        pnlIdV.setLayout(pnlIdVLayout);
        pnlIdVLayout.setHorizontalGroup(
            pnlIdVLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 196, Short.MAX_VALUE)
        );
        pnlIdVLayout.setVerticalGroup(
            pnlIdVLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 16, Short.MAX_VALUE)
        );

        pnl6.add(pnlIdV, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 20, 200, 20));

        jScrollPane1.setViewportView(pnl6);

        pnlMGY.setBorder(javax.swing.BorderFactory.createTitledBorder("Gợi ý trả lời"));

        pnlHN.setBorder(javax.swing.BorderFactory.createTitledBorder("Hàng ngang"));

        pnlRowH.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.UIManager.getDefaults().getColor("Panel.background"), javax.swing.UIManager.getDefaults().getColor("Panel.background")));

        org.jdesktop.layout.GroupLayout pnlRowHLayout = new org.jdesktop.layout.GroupLayout(pnlRowH);
        pnlRowH.setLayout(pnlRowHLayout);
        pnlRowHLayout.setHorizontalGroup(
            pnlRowHLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 119, Short.MAX_VALUE)
        );
        pnlRowHLayout.setVerticalGroup(
            pnlRowHLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 169, Short.MAX_VALUE)
        );

        pnlGYH.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.UIManager.getDefaults().getColor("Panel.background"), javax.swing.UIManager.getDefaults().getColor("Panel.background")));

        org.jdesktop.layout.GroupLayout pnlGYHLayout = new org.jdesktop.layout.GroupLayout(pnlGYH);
        pnlGYH.setLayout(pnlGYHLayout);
        pnlGYHLayout.setHorizontalGroup(
            pnlGYHLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 265, Short.MAX_VALUE)
        );
        pnlGYHLayout.setVerticalGroup(
            pnlGYHLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 169, Short.MAX_VALUE)
        );

        pnlAH.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.UIManager.getDefaults().getColor("Panel.background"), javax.swing.UIManager.getDefaults().getColor("Panel.background")));

        org.jdesktop.layout.GroupLayout pnlAHLayout = new org.jdesktop.layout.GroupLayout(pnlAH);
        pnlAH.setLayout(pnlAHLayout);
        pnlAHLayout.setHorizontalGroup(
            pnlAHLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 460, Short.MAX_VALUE)
        );
        pnlAHLayout.setVerticalGroup(
            pnlAHLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 169, Short.MAX_VALUE)
        );

        org.jdesktop.layout.GroupLayout pnlHNLayout = new org.jdesktop.layout.GroupLayout(pnlHN);
        pnlHN.setLayout(pnlHNLayout);
        pnlHNLayout.setHorizontalGroup(
            pnlHNLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlHNLayout.createSequentialGroup()
                .addContainerGap()
                .add(pnlRowH, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(pnlGYH, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(pnlAH, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnlHNLayout.setVerticalGroup(
            pnlHNLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlHNLayout.createSequentialGroup()
                .add(pnlHNLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(pnlAH, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(pnlRowH, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, pnlGYH, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pnlHD.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder("Hàng dọc")));
        pnlHD.setAutoscrolls(true);

        pnlGYV.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.UIManager.getDefaults().getColor("Panel.background"), javax.swing.UIManager.getDefaults().getColor("Panel.background")));

        org.jdesktop.layout.GroupLayout pnlGYVLayout = new org.jdesktop.layout.GroupLayout(pnlGYV);
        pnlGYV.setLayout(pnlGYVLayout);
        pnlGYVLayout.setHorizontalGroup(
            pnlGYVLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 265, Short.MAX_VALUE)
        );
        pnlGYVLayout.setVerticalGroup(
            pnlGYVLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 156, Short.MAX_VALUE)
        );

        pnlRowV.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.UIManager.getDefaults().getColor("Panel.background"), javax.swing.UIManager.getDefaults().getColor("Panel.background")));

        org.jdesktop.layout.GroupLayout pnlRowVLayout = new org.jdesktop.layout.GroupLayout(pnlRowV);
        pnlRowV.setLayout(pnlRowVLayout);
        pnlRowVLayout.setHorizontalGroup(
            pnlRowVLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 116, Short.MAX_VALUE)
        );
        pnlRowVLayout.setVerticalGroup(
            pnlRowVLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 156, Short.MAX_VALUE)
        );

        pnlAV.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.UIManager.getDefaults().getColor("Panel.background"), javax.swing.UIManager.getDefaults().getColor("Panel.background")));

        org.jdesktop.layout.GroupLayout pnlAVLayout = new org.jdesktop.layout.GroupLayout(pnlAV);
        pnlAV.setLayout(pnlAVLayout);
        pnlAVLayout.setHorizontalGroup(
            pnlAVLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 455, Short.MAX_VALUE)
        );
        pnlAVLayout.setVerticalGroup(
            pnlAVLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 156, Short.MAX_VALUE)
        );

        org.jdesktop.layout.GroupLayout pnlHDLayout = new org.jdesktop.layout.GroupLayout(pnlHD);
        pnlHD.setLayout(pnlHDLayout);
        pnlHDLayout.setHorizontalGroup(
            pnlHDLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlHDLayout.createSequentialGroup()
                .addContainerGap()
                .add(pnlRowV, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(pnlGYV, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(pnlAV, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnlHDLayout.setVerticalGroup(
            pnlHDLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlHDLayout.createSequentialGroup()
                .add(pnlHDLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(pnlAV, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(pnlRowV, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, pnlGYV, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        org.jdesktop.layout.GroupLayout pnlMGYLayout = new org.jdesktop.layout.GroupLayout(pnlMGY);
        pnlMGY.setLayout(pnlMGYLayout);
        pnlMGYLayout.setHorizontalGroup(
            pnlMGYLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, pnlMGYLayout.createSequentialGroup()
                .add(pnlMGYLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(pnlHD, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(pnlHN, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        pnlMGYLayout.setVerticalGroup(
            pnlMGYLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlMGYLayout.createSequentialGroup()
                .add(pnlHN, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(pnlHD, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(35, Short.MAX_VALUE))
        );

        jScrollPane2.setViewportView(pnlMGY);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 360, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 806, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 301, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 405, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtTitleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtTitleActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtTitleActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
        JTextField temp=null;
        JLabel lbl=null;
        boolean b=false;
        int n=(_width<_height)?_height:_width;
        String str="";
        for(int i=0;i<n;i++)
            str+="*";
        for(int i=0;i<_width;i++)//kiểm tra hàng ngang
        {
            temp=(JTextField)pnlGYH.getComponent(i);
            lbl=(JLabel)pnlAH.getComponent(i);
            if(!lbl.getText().substring(lbl.getText().length()-_height).equalsIgnoreCase(str.substring(0,_height))&&temp.getText().equals(""))
            {
                //JOptionPane.showConfirmDialog(this, lbl.getText().substring(lbl.getText().length()-_height)+"   "+(str.substring(0,_height)));
                JOptionPane.showMessageDialog(this, "Chưa nhập gợi ý cho hàng "+(i+1)+"  "+lbl.getText().substring(lbl.getText().length()-_height));
                return;
            }
        }
        for(int i=0;i<_height;i++)//kiểm tra cột
        {
            temp=(JTextField)pnlGYV.getComponent(i);
            lbl=(JLabel)pnlAV.getComponent(i);
//            if(temp.getText().equals(""))
//                JOptionPane.showConfirmDialog(this,true);
            if(!lbl.getText().substring(lbl.getText().length()-_width).equalsIgnoreCase(str.substring(0,_width))&&temp.getText().equals(""))
            {
                //JOptionPane.showConfirmDialog(this, lbl.getText().substring(lbl.getText().length()-_height)+"   "+(str.substring(0,_height)));
                JOptionPane.showMessageDialog(this, "Chưa nhập gợi ý cho cột "+(i+1));
                return;
            }
        }
        n=DB.InsertQ(_width, _height, txtTitle.getText());
        for(int i=0;i<_width;i++)//kiểm tra hàng ngang
        {
            temp=(JTextField)pnlGYH.getComponent(i);
            lbl=(JLabel)pnlAH.getComponent(i);
            if(!temp.getText().trim().equals(""))
                DB.InsertDetailQ(n,temp.getText(), lbl.getText().substring(lbl.getText().length()-_height), i);
        }
        for(int i=0;i<_height;i++)//kiểm tra hàng ngang
        {
            temp=(JTextField)pnlGYV.getComponent(i);
            lbl=(JLabel)pnlAV.getComponent(i);
            if(!temp.getText().trim().equals(""))
                DB.InsertDetailQ(n,temp.getText(),lbl.getText().substring(lbl.getText().length()-_width), i+_width);
        }
        JOptionPane.showMessageDialog(this, "Thêm thành công.");
        LoadAll();
}//GEN-LAST:event_btnAddActionPerformed
    String[][] getRange()
    {
        String[][] s=new String[2][_height];
        for(int i=0;i<_width*_height;i++)
        {
            //String[i%_wi]
        }
        return s;
    }
    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
        // TODO add your handling code here:
       LoadAll();
}//GEN-LAST:event_btnResetActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
        
    }//GEN-LAST:event_formWindowOpened

    private void txtHKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtHKeyReleased
        // TODO add your handling code here:
        LoadAll();
}//GEN-LAST:event_txtHKeyReleased

    private void txtVKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtVKeyReleased
        // TODO add your handling code here:
        LoadAll();
}//GEN-LAST:event_txtVKeyReleased

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new frmAddQ().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnReset;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JPanel pnl6;
    private javax.swing.JPanel pnlAH;
    private javax.swing.JPanel pnlAV;
    private javax.swing.JPanel pnlGYH;
    private javax.swing.JPanel pnlGYV;
    private javax.swing.JPanel pnlHD;
    private javax.swing.JPanel pnlHN;
    private javax.swing.JPanel pnlIdH;
    private javax.swing.JPanel pnlIdV;
    private javax.swing.JPanel pnlM;
    private javax.swing.JPanel pnlMGY;
    private javax.swing.JPanel pnlRowH;
    private javax.swing.JPanel pnlRowV;
    private javax.swing.JTextField txtH;
    private javax.swing.JTextField txtTitle;
    private javax.swing.JTextField txtV;
    // End of variables declaration//GEN-END:variables
    
}
